package weifei.live.simulation.im.cmd;


import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.Getter;

import java.util.Date;


/**
 * {
 *     "type":"Message",
 *     "format":"Text",
 *     "destination":"UserToFriend",
 *     "senderId":1,
 *     "targetIds":[2],
 *     "content":"你好",
 *     "sendTime":null
 * }
 * */


@Data
public class Message
{
    /** Notice会弹窗 */
    @Getter
    public enum Type
    {
        Notice(1),     // 通知，只有系统才有权限发送
        Message(2);    // 消息，系统消息使用一个专门的聊天页面

        @EnumValue
        private final int id;
        Type(int id) { this.id = id; }
    }

    @Getter
    public enum Format
    {
        Text(1),
        Voice(2),
        Image(3);       // 使用base64编码

        @EnumValue
        private final int id;
        Format(int id) { this.id = id; }
    }


    public enum Destination
    {
        Adminer,        // 服务器发给管理员的消息
        Visitor,        // 游客，系统返回Message
        User,           // 系统给用户的可以是Notice或者Message，广播给所有用户或者通知某些用户
        Group,          // 广播给所有群聊或者某些群聊，targetIds 为 null 则为广播给所有群
        UserToFriend,   // 用户发给朋友的消息，只能是Message
        UserToGroup;    // 用户发送到群聊的消息，只能是Message
    }


    public Message.Type type;
    private Message.Format format;
    private Message.Destination destination;
    private Integer senderId = null;            // null代表服务器，管理员有ID
    private Integer[] targetIds;
    private String content;
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private java.util.Date sendTime = new Date();


    public Message() {}
    public Message(Message message) {
        this.type = message.type;
        this.format = message.format;
        this.destination = message.destination;
        this.senderId = message.senderId;
        this.targetIds = message.targetIds;
        this.content = message.content;
        this.sendTime = message.sendTime;
    }
    public Message(
            Type type,
            Format format,
            Destination destination,
            Integer senderId,
            Integer[] targetIds,
            String content
    )
    {
        this(type, format, destination, senderId, targetIds, content, new Date());
    }


    public Message(
            Type type,
            Format format,
            Destination destination,
            Integer senderId,
            Integer[] targetIds,
            String content,
            java.util.Date sendTime
    )
    {
        this.type = type;
        this.format = format;
        this.destination = destination;
        this.senderId = senderId;
        this.targetIds = targetIds;
        this.content = content;
        this.sendTime = sendTime;
    }


    public static Message fromJson(String content)
            throws JsonProcessingException
    {
        return new ObjectMapper().readValue(content, Message.class);
    }


    public static Message toAdminerTextNotice(Integer[] targetIds, String content)
    {
        return new Message(Type.Notice, Format.Text, Destination.Adminer, null, targetIds, content);
    }
    public static Message toAdminerTextMessage(Integer[] targetIds, String content)
    {
        return new Message(Type.Message, Format.Text, Destination.Adminer, null, targetIds, content);
    }


    public static Message toVisitorTextMessage(String content)
    {
        return new Message(Type.Message, Message.Format.Text, Destination.Visitor, null, null, content);
    }

    public static Message toUserTextNotice(Integer[] targetIds, String content)
    {
        return new Message(Type.Notice, Message.Format.Text, Message.Destination.User, null, targetIds, content);
    }

    public static Message toUserTextMessage(Integer[] targetIds, String content)
    {
        return new Message(Type.Message, Message.Format.Text, Message.Destination.User, null, targetIds, content);
    }
}


